package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.catalina.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import dao.FoodDao;
import vo.Food;
@Repository("fd")
public class FoodDaoImpl implements FoodDao {
	@PersistenceContext(name="un")
	private EntityManager em;
	@Override
	@Transactional
	public int addFood(Food f) {
		int rs = 0;
		em.persist(f);
		return 1;
	}

	@Override
	public List<Food> findAllFood(Integer mId) {
		
		String jpql = "select f from Food f where f.mId=:mId";
		List<Food> listFood = em.createQuery(jpql)
				.setParameter("mId", mId).getResultList();
		if(listFood.isEmpty()){
			return null;
		}else{
			return listFood;
		}
	}

	@Override
	public List<Food> findRecommendFood(Integer mId, Integer fStatus) {
		String jpql = "select f from Food f where f.mId=:mId and f.fStatus=:fStatus";
		List<Food> listFood = em.createQuery(jpql)
				.setParameter("mId", mId)
				.setParameter("fStatus", fStatus)
				.getResultList();
		if(listFood.isEmpty()){
			return null;
		}
		else{
			return listFood;
		}
	}

	@Override
	@Transactional
	public int delFood(Integer fId) {
		Food f = em.find(Food.class, fId);
		em.remove(f);
		return 1;
	}

	@Override
	@Transactional
	public int updateFood(Food f) {
		Food food = em.find(Food.class, f.getfId());
		food.setfName(f.getfName());
		food.setfPrice(f.getfPrice());
		food.setfStatus(f.getfStatus());
		food.setfViewPath(f.getfViewPath());
		food.setmId(f.getmId());
		return 1;
	}
	

}
